package com.ymatou.iapi.sellerproduct.testcase;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.ActivityProductSuspendSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;


public class Ts_ActivityProductSuspendSale {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播商品暂停销售");
	}
	
	@Before
	public void caseUp() {
		
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ActivityProductSuspendSaleCall().getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_ActivityProductSuspendSale_001() {
		Logger.start(true,"直播商品暂停销售");
		try{
			//1 add new product
			JSONObject productReqBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductID");
			
			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live 正在进行的直播
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 suspend sale [SellStatus=2]
			JSONObject suspendSaleBean = SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId);
			Logger.verifyEquals( 200,suspendSaleBean.getInt("Code"), "响应报文-返回码");
			
			YMTDateUtil.waitTime(1);
			
			//check sql
			Map<String, Object> liveInProductBean = new YmtProductsInLiveWapper().selectInLiveByProductId(productId).get(0);
			AssertService.assertResultEqual(MapUtil.hashMap("iSellStatus",2), liveInProductBean);
			

			
			//check mongo
			if(YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				AssertService.assertResultEqual(MapUtil.hashMap("SellStatus", 2), liveProductInfoMap);
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//Mongo - LiveProductModel
				Boolean iSellStatus = YmtMongoDriverFactroy.isUpdate("LiveProductModel", MapUtil.hashMap("LiveId", liveId), MapUtil.hashMap("SellStatus", 2));
				Logger.verifyEquals(true, iSellStatus, "LiveProductModel SellStatus 应为2");
			}
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Map<String, Object> map = new LiveProductsWapper().getByActivityId(liveId);
				Map<String, Object> tgtMap = MapUtil.hashMap("status", 2, "istop", false);
				AssertService.assertResultEqual(tgtMap, map,"Mongo-Liveproducts 暂停销售检查");
			}
			
			//新品时间检查
			SellerProductCallService.checkListingTime(productId, YMTDateUtil.getDate());
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_ActivityProductSuspendSale_002() {
		Logger.start(false,"将未开始直播的商品从开始销售状态变为暂停销售");
		try{
			//1 add new product
			JSONObject productReqBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductID");
			
			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveBeginInFuture(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live 正在进行的直播
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			
			//4 suspend sale [SellStatus=2]
			JSONObject suspendSaleBean = SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId);
			Logger.verifyEquals( 200,suspendSaleBean.getInt("Code"), "响应报文-返回码");		
			
			//check sql
			Map<String, Object> liveInProductBean = new YmtProductsInLiveWapper().selectInLiveByProductId(productId).get(0);
			AssertService.assertResultEqual(MapUtil.hashMap("iSellStatus",2), liveInProductBean);
			

			
			//check mongo
			if(YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				AssertService.assertResultEqual(MapUtil.hashMap("SellStatus", 2), liveProductInfoMap);
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//Mongo - LiveProductModel
				Boolean iSellStatus = YmtMongoDriverFactroy.isUpdate("LiveProductModel", MapUtil.hashMap("LiveId", liveId), MapUtil.hashMap("SellStatus", 2));
				Logger.verifyEquals(true, iSellStatus, "LiveProductModel SellStatus 应为2");
			}
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Map<String, Object> map = new LiveProductsWapper().getByActivityId(liveId);
				Map<String, Object> tgtMap = MapUtil.hashMap("status", 2, "istop", false);
				AssertService.assertResultEqual(tgtMap, map,"Mongo-Liveproducts 暂停销售检查");
			}
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_ActivityProductSuspendSale_003() {
		Logger.start(false,"将已结束直播的商品从开始销售状态变为暂停销售");
		try{
			//1 add new product
			JSONObject productReqBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductID");
			
			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live 正在进行的直播
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 close live
			LiveCallService.closeLive(liveId, sellerId);
			
			//5 suspend sale [SellStatus=2]
			JSONObject suspendSaleBean = SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId);
			Logger.verifyEquals( 200,suspendSaleBean.getInt("Code"), "响应报文-返回码");		
			
			//check sql
			Map<String, Object> liveInProductBean = new YmtProductsInLiveWapper().selectInLiveByProductId(productId).get(0);
			AssertService.assertResultEqual(MapUtil.hashMap("iSellStatus",2), liveInProductBean);			
			
			//check mongo
			if(YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				AssertService.assertResultEqual(MapUtil.hashMap("SellStatus", 2), liveProductInfoMap);
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//Mongo - LiveProductModel
				Boolean iSellStatus = YmtMongoDriverFactroy.isUpdate("LiveProductModel", MapUtil.hashMap("LiveId", liveId), MapUtil.hashMap("SellStatus", 2));
				Logger.verifyEquals(true, iSellStatus, "LiveProductModel SellStatus 应为2");
			}
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Map<String, Object> map = new LiveProductsWapper().getByActivityId(liveId);
				Map<String, Object> tgtMap = MapUtil.hashMap("status", 2, "istop", false);
				AssertService.assertResultEqual(tgtMap, map,"Mongo-Liveproducts 暂停销售检查");
			}
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}		
	
	
	
	
	
	
	
	
	
	
	
	
	
}
